package com.analysis.controller;

import com.analysis.dto.ApiResponse;
import com.analysis.entity.Company;
import com.analysis.entity.Version;
import com.analysis.enums.RequireLogin;
import com.analysis.service.CompanyService;
import com.analysis.service.CompanyVersionRefService;
import com.analysis.service.VersionService;
import com.analysis.utils.UserUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/company")
@CrossOrigin(origins = "*")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private VersionService versionService;

    @PostMapping("/add")
    @RequireLogin
    public ApiResponse<Company> add(@RequestBody Company company) {
        try {
            if (company.getName() == null || company.getName().trim().isEmpty()) {
                return ApiResponse.error("公司名称不能为空");
            }
            Company exists = companyService.getByName(company.getName());
            if (exists != null) {
                return ApiResponse.error("公司已存在");
            }
            int result = companyService.create(company);
            if (result > 0) {
                return ApiResponse.success(company);
            }
            return ApiResponse.error("创建公司失败");
        } catch (Exception e) {
            return ApiResponse.error("创建公司异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @RequireLogin
    public ApiResponse<String> delete(@RequestParam Long id, HttpServletRequest request) {
        try {
            Long companyId = UserUtils.getCurrentUser(request).getCompanyId();
            Company company = companyService.getById(id);
            if (company == null || !company.getId().equals(companyId)) {
                return ApiResponse.error("公司不存在或无权限删除");
            }

            int result = companyService.delete(id);
            if (result > 0) {
                return ApiResponse.success("删除成功");
            }
            return ApiResponse.error("删除失败或不存在");
        } catch (Exception e) {
            return ApiResponse.error("删除公司异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    @RequireLogin
    public ApiResponse<String> update(@RequestBody Company company) {
        try {
            if (company.getId() == null) {
                return ApiResponse.error("ID不能为空");
            }
            int result = companyService.update(company);
            if (result > 0) {
                return ApiResponse.success("更新成功");
            }
            return ApiResponse.error("更新失败");
        } catch (Exception e) {
            return ApiResponse.error("更新公司异常: " + e.getMessage());
        }
    }

    @GetMapping("/get")
    @RequireLogin
    public ApiResponse<Company> get(@RequestParam Long id) {
        try {
            Company company = companyService.getById(id);
            if (company == null) {
                return ApiResponse.error("公司不存在或无权限删除");
            }
            return ApiResponse.success(company);
        } catch (Exception e) {
            return ApiResponse.error("查询公司异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @RequireLogin
    public ApiResponse<List<Company>> list() {
        try {
            return ApiResponse.success(companyService.listAll());
        } catch (Exception e) {
            return ApiResponse.error("查询公司列表异常: " + e.getMessage());
        }
    }

    /**
     * 查询公司开通的版本信息
     */
    @GetMapping("/version")
    @RequireLogin
    public ApiResponse<Version> getCompanyVersion(HttpServletRequest request) {
        try {
            // 验证权限：只能查询当前用户所属公司的版本信息
            Long currentCompanyId = UserUtils.getCurrentUser(request).getCompanyId();
            Version version = versionService.getVersionsByCompanyId(currentCompanyId);
            return ApiResponse.success(version);
        } catch (Exception e) {
            return ApiResponse.error("查询公司版本信息异常: " + e.getMessage());
        }
    }

    
}


